import java.util.Arrays;

public class string {
    //1.String构造函数
    public static void test1()
    {
        String str1 = "hello";
        System.out.println(str1);

        String str2 = new String();
        System.out.println(str2);

        String str3 = new String("pppp");
        System.out.println(str3);

        char[] array = {'a','b','c'};
        String str4 = new String(array,1,2);
        System.out.println(str4);
    }

    public static void test2()
    {
        String str = "hello";
        //2.length: 字符串长度
        System.out.println(str.length());

        //3.isEmpty: 判断字符串是否为空
        System.out.println(str.isEmpty());
    }

    public static void test3()
    {
        String s1 = new String("abc");
        String s2 = new String("ABC");

        //4.equals: 比较内容是否相同，返回值为true和false
        System.out.println(s1.equals(s2));

        //5.compareTo: 比较内容是否相同,
        //  正数 s1 > s2
        //  0 s1==s2
        //  负数 s1 < s2
        System.out.println(s1.compareTo(s2));

        //6.compareToIgnoreCase: 忽略大小写比较
        //  正数 s1 > s2
        //  0 s1==s2
        //  负数 s1 < s2
        System.out.println(s1.compareToIgnoreCase(s2));
    }

    public static void test4()
    {
        String s = "HeLlo";

        //7.toUpperCase: 转为大写
        System.out.println(s.toUpperCase());

        //8.toLowerCase: 转为小写
        System.out.println(s.toLowerCase());

        //9.valueOf: 转为字符串
        System.out.println(String.valueOf(12));

        //10.indexOf: 从前往后在指定位置开始找一个字符或字符串
        System.out.println(s.indexOf("L",2));

        //11.lastIndexOf: 从后往前在指定位置开始找一个字符或字符串
        s.lastIndexOf("H",s.length()-1);

        //12.charAt: 获取下标对应的字符
        System.out.println(s.charAt(0));
    }

    public static void test5()
    {
        String s = "hello";

        //13.toCharArray: 将字符串转为字符数组
        System.out.println(Arrays.toString(s.toCharArray()));

        //14.format: 格式化字符串
        String s1 = String.format("%d年%d月%d日", 2019,1,1);
        System.out.println(s1);
    }

    public static void test6()
    {
        String str = "ababcabcdabcde";

        //15.replace: 将一个字符串中指定字符串换成另一个字符串
        System.out.println(str.replace("ab", "9999"));

        //16.replaceFirst: 只将第一个进行变换
        System.out.println(str.replaceFirst("ab", "9999"));

        //17.replaceAll: 基于正则表达式进行转换
        System.out.println(str.replaceAll("ab", "124"));
        System.out.println(str);
    }

    public static void test7()
    {
        String str = "hello abc world";

        //18.split: 用regex分割字符串, 分成limit份
        String[] ret = str.split(" ",2);
        for(String s : ret)
        {
            System.out.println(s);
        }
    }

    public static void test8()
    {
        String str = "ababc";
        //19.substring: 分割字符串[0,3)
        System.out.println(str.substring(0, 3));

        //20.trim: 去掉左右两边的空格
        str = "       a    b   c       ";
        System.out.println(str.trim());
    }
    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
//        test6();
//        test7();
        test8();

    }
}
